<!-- 属性分组的新增、修改 -->
<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    @closed="dialogClose"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="90px"
    >
      <el-form-item label="组名" prop="attrGroupName">
        <el-input
          v-model="dataForm.attrGroupName"
          placeholder="组名"
        ></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="dataForm.sort" placeholder="排序"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="descript">
        <el-input v-model="dataForm.descript" placeholder="描述"></el-input>
      </el-form-item>
      <el-form-item label="组图标" prop="icon">
        <el-input v-model="dataForm.icon" placeholder="组图标"></el-input>
      </el-form-item>
      <el-form-item label="所属分类id" prop="categoryId">
        <!-- filterable 打开搜索功能 -->
        <!-- <el-cascader
          v-model="dataForm.categoryPath"
          :options="categoryList"
          :props="props"
          filterable
        ></el-cascader> -->
        <category-cascader
          :categoryPath.sync="categoryPath"
        ></category-cascader>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button
        type="primary"
        @click="dataFormSubmit()"
        :disabled="saveBtnDisabled"
      >
        确定
      </el-button>
    </span>
  </el-dialog>
</template>

<script>
import CategoryCascader from '../common/category-cascader'
export default {
  components: { CategoryCascader },
  data() {
    return {
      props: {
        value: 'catId',
        label: 'name',
        children: 'childrenList',
      },
      saveBtnDisabled: false,
      categoryList: [],
      categoryPath: [],
      visible: false,
      dataForm: {
        attrGroupId: 0,
        attrGroupName: '',
        sort: '',
        descript: '',
        icon: '',
        categoryId: '',
      },
      dataRule: {
        attrGroupName: [
          { required: true, message: '组名不能为空', trigger: 'blur' },
        ],
        sort: [{ required: true, message: '排序不能为空', trigger: 'blur' }],
        descript: [
          { required: true, message: '描述不能为空', trigger: 'blur' },
        ],
        icon: [{ required: true, message: '组图标不能为空', trigger: 'blur' }],
        categoryId: [
          { required: true, message: '所属分类id不能为空', trigger: 'blur' },
        ],
      },
    }
  },
  watch: {
    categoryPath(path) {
      //监听到路径变化
      console.log('路径变了', path)
      this.dataForm.categoryId = path[path.length - 1]
    },
  },

  created() {
    this.getCategoryList()
  },

  methods: {
    // 关闭对话框时，清空所选分类
    dialogClose() {
      this.categoryPath = []
    },
    getCategoryList() {
      this.$http({
        url: this.$http.adornUrl('/product/category/list/tree'),
        method: 'get',
      }).then(({ data }) => {
        this.categoryList = data.data
      })
    },
    init(id) {
      this.dataForm.attrGroupId = id || 0
      this.visible = true
      this.saveBtnDisabled = false
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.attrGroupId) {
          this.$http({
            url: this.$http.adornUrl(
              `/product/attrGroup/info/${this.dataForm.attrGroupId}`
            ),
            method: 'get',
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.attrGroupName = data.attrGroup.attrGroupName
              this.dataForm.sort = data.attrGroup.sort
              this.dataForm.descript = data.attrGroup.descript
              this.dataForm.icon = data.attrGroup.icon
              this.dataForm.categoryId = data.attrGroup.categoryId
              this.categoryPath = data.attrGroup.categoryPath
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid && !this.saveBtnDisabled) {
          this.saveBtnDisabled = true
          this.$http({
            url: this.$http.adornUrl(
              `/product/attrGroup/${
                !this.dataForm.attrGroupId ? 'save' : 'update'
              }`
            ),
            method: 'post',
            data: this.$http.adornData({
              attrGroupId: this.dataForm.attrGroupId || undefined,
              attrGroupName: this.dataForm.attrGroupName,
              sort: this.dataForm.sort,
              descript: this.dataForm.descript,
              icon: this.dataForm.icon,
              categoryId: this.categoryPath[this.categoryPath.length - 1],
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                },
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    },
  },
}
</script>
